package com.ld.manageBook.service.impl;

import com.ld.manageBook.DAO.BorrowDAO;
import com.ld.manageBook.DAO.BorrowStatusDAO;
import com.ld.manageBook.bean.BorrowStatus;
import com.ld.manageBook.service.BorrowStatusService;

public class BorrowStatusImpl implements BorrowStatusService{
	private BorrowStatusDAO borrowStatusDAO = new BorrowStatusDAO();

	@Override
	public BorrowStatus getBorrowStatusByUserName(String userName) {
		return borrowStatusDAO.querySingle("select * from borrowstatus where user_name = ?",BorrowStatus.class,userName);
	}

	@Override
	public int updateStatus(String userName, Integer number) {
		System.out.println(userName);
		BorrowStatus borrowStatusByUserName = getBorrowStatusByUserName(userName);
		if(borrowStatusByUserName != null){
			return borrowStatusDAO.update("update borrowstatus set borrow_books = ? where `user_name` = ? ",borrowStatusByUserName.getBorrow_books() + number,userName);
		}else {
			return borrowStatusDAO.update("insert into borrowstatus(`user_name`,borrow_books) values(?,?)",userName,number);
		}

	}

	@Override
	public int returnBorrowBook(String userName, Integer returnNumber) {
		BorrowStatus borrowStatusByUserName = getBorrowStatusByUserName(userName);
		return borrowStatusDAO.update("update borrowstatus set borrow_books = ? where user_name = ?",borrowStatusByUserName.getBorrow_books() - returnNumber,userName);
	}
}
